//
//  PPIncomeTreeCell.m
//  PPDL
//
//  Created by 王洋 on 2019/8/7.
//  Copyright © 2019 王洋. All rights reserved.
//

#import "PPIncomeTreeCell.h"
#import "TreeView.h"

@interface PPIncomeTreeCell ()<TreeViewDelegate>


@property (nonatomic, strong) TreeView *treeView;

//师徒列表
@property (nonatomic, strong) UIButton *apprenticeListBtn;


@property (nonatomic, strong) UIButton *collectBtn;


@property (nonatomic, strong) UIButton *countBtn;


@property (nonatomic, strong) NSMutableArray <IncomeItemModel *>* dataArray;


@property (nonatomic, assign) double bounty;


@property (nonatomic, assign) NSInteger clickCount;//点击次数

@end

@implementation PPIncomeTreeCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        [self setClipsToBounds:YES];
        [self layout];
    }
    return self;
}

- (void)fillCellWithArray:(NSMutableArray <IncomeItemModel *>*)array AndBounty:(double)bounty {

    self.dataArray = array;
    self.bounty = bounty;
    [_treeView removeAllRandomBtn];
    _treeView.incomeList = array;
 
    [self adjustCountBtnWith:bounty];
}

- (void)adjustCountBtnWith:(double)bounty {
    
    if (bounty >=0) {
        //如果是整数，那后面就不显示两位小数
        
        [self.countBtn setTitle:[Utilities changeDoubleToString:bounty] forState:UIControlStateNormal];

    }
}

#pragma mark - TreeViewDelegate

- (void)selectTimeLimitedBtnAtIndex:(NSInteger)index
{
    [_treeView removeRandomIndex:index];
}

- (void)selectUnlimitedBtnAtIndex:(NSInteger)index
{
    [_treeView removeRandomIndex:index];
    if (self.incomeCollectBlock) {
        self.incomeCollectBlock(index);
    }
    
    [Utilities playSoundWithFileName:@"simpleCoin" fileType:@"wav"];
    
    //点击后刷新数字
    double count = [self.dataArray[index].incomeCount doubleValue];
    self.bounty = self.bounty + count;
    [self adjustCountBtnWith:self.bounty];
    
    self.clickCount ++;
    if (self.clickCount >= 10) {
        self.collectBtn.hidden = NO;
    }
}


- (void)allCollected
{
    if (self.incomeCollectFinishBlock) {
        self.incomeCollectFinishBlock();
    }
    
    self.collectBtn.hidden = NO;
}

#pragma mark - btnClick

//收取所有
- (void)collectAll {
    [self.treeView removeAllRandomBtn];
    
    if (self.incomeCollectAllBlock) {
        self.incomeCollectAllBlock();
    }
}

//去师徒页面
- (void)goApprenticeList {
    
    if (self.goApprenticeListBlock) {
        self.goApprenticeListBlock();
    }
}

- (UIButton *)countBtn {
    
    if (!_countBtn) {
        _countBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_countBtn setBackgroundImage:[UIImage imageNamed:@"tree_box"] forState:UIControlStateNormal];
        [_countBtn setTitle:@"X" forState:UIControlStateNormal];
        _countBtn.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];

        [_countBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    }
    return _countBtn;
}

- (UIButton *)collectBtn {
    
    if (!_collectBtn) {
        _collectBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_collectBtn setImage:[UIImage imageNamed:@"tree_all"] forState:UIControlStateNormal];
        [_collectBtn addTarget:self action:@selector(collectAll) forControlEvents:UIControlEventTouchUpInside];
    }
    return _collectBtn;
}

- (UIButton *)apprenticeListBtn {

    if (!_apprenticeListBtn) {
        _apprenticeListBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_apprenticeListBtn setImage:[UIImage imageNamed:@"tree_shitu"] forState:UIControlStateNormal];
        [_apprenticeListBtn addTarget:self action:@selector(goApprenticeList) forControlEvents:UIControlEventTouchUpInside];
    }
    return _apprenticeListBtn;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (NSMutableArray<IncomeItemModel *> *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray new];
    }
    return _dataArray;
}

- (void)layout {
    
    TreeView *treeView = [[TreeView alloc] initWithFrame:CGRectMake(0, 0, TTScreenWidth, TTScreenHeight - kNavBarAndStatusBarHeight - kTabBarHeight)];
    treeView.delegate = self;
    _treeView = treeView;
    
    self.clickCount = 0;
    self.collectBtn.hidden = YES;
    
    [self addSubview:self.treeView];
        
    [self addSubview:self.collectBtn];
//    [self addSubview:self.apprenticeListBtn];
    [self addSubview:self.countBtn];
    
//    [self.apprenticeListBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(-20);
//        make.top.mas_equalTo(20);
//    }];
    
//    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(-20);
//        make.top.mas_equalTo(30);
//    }];
    
    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.top.mas_equalTo(30);
    }];
    
    CGFloat toRight = 84 ;
    CGFloat toBottom = 76 ;
    CGFloat width = 140;
    if (IS_IPHONE_4_OR_LESS) {
        toRight = 30;
        toBottom = 30;
    } else if (IS_IPHONE_5) {
        toRight = 30;
        toBottom = 10;
        width = 100;
    } else if (IS_IPHONE_6) {
        toRight = 40;
        toBottom = 30;
        width = 100;
    } else if (IS_IPHONE_6P) {
        toRight = 40;
        toBottom = 40;
        width = 120;
    } else {
        toRight = 50;
        toBottom = 50;
        width = 120;
    }
    
    [self.countBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-toBottom);
        make.right.mas_equalTo(-toRight);
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(width);
    }];
}

@end
